package com.kawa.kemanage.controller.api;

import com.kawa.kemanage.entity.AdCampaign;
import com.kawa.kemanage.service.AdCampaignService;
import com.kawa.kemanage.util.Result;
import com.kawa.kemanage.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 广告活动管理控制器
 */
@RestController
@RequestMapping("/api/campaigns")
public class AdCampaignController {
    @Autowired
    private AdCampaignService adCampaignService;

    /**
     * 查询广告计划列表（分页）
     * @param page 当前页码
     * @param size 每页显示条数
     * @return 分页结果
     */
    @GetMapping
    public Result<PageResult<AdCampaign>> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(adCampaignService.findByPage(page, size));
    }

    /**
     * 根据ID查询广告计划
     * @param id 广告计划ID
     * @return 广告计划信息
     */
    @GetMapping("/{id}")
    public Result<AdCampaign> findById(@PathVariable Integer id) {
        return Result.success(adCampaignService.findById(id));
    }

    /**
     * 根据广告主ID查询广告计划（分页）
     * @param advertiserId 广告主ID
     * @param page 当前页码
     * @param size 每页显示条数
     * @return 分页结果
     */
    @GetMapping("/advertiser/{advertiserId}")
    public Result<PageResult<AdCampaign>> findByAdvertiserId(
            @PathVariable Integer advertiserId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(adCampaignService.findByAdvertiserIdAndPage(advertiserId, page, size));
    }

    /**
     * 创建新的广告计划
     * @param campaign 广告计划信息
     * @return 创建后的广告计划信息
     */
    @PostMapping
    public Result<AdCampaign> create(@RequestBody AdCampaign campaign) {
        return Result.success(adCampaignService.create(campaign));
    }

    /**
     * 更新广告计划信息
     * @param id 广告计划ID
     * @param campaign 更新的广告计划信息
     * @return 更新后的广告计划信息
     */
    @PutMapping("/{id}")
    public Result<AdCampaign> update(@PathVariable Integer id, @RequestBody AdCampaign campaign) {
        return Result.success(adCampaignService.update(id, campaign));
    }

    /**
     * 删除广告计划
     * @param id 广告计划ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        adCampaignService.delete(id);
        return Result.success();
    }

    /**
     * 查询已删除的广告计划列表（分页）
     * @param page 当前页码
     * @param size 每页显示条数
     * @return 分页结果
     */
    @GetMapping("/deleted")
    public Result<PageResult<AdCampaign>> findDeleted(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(adCampaignService.findDeletedByPage(page, size));
    }
}
